package cn.cy.web;

import cn.cy.pojo.SysEmployee;
import cn.cy.service.SysEmployeeService;
import cn.cy.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Controller("employeeAction")
public class EmployeeAction extends ActionSupport {
    private  static final Logger log  = Logger.getLogger(EmployeeAction.class);
    @Autowired
    private SysEmployeeService sysEmployeeService;
    private SysEmployee employee;
    public String login(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        if(employee != null){
            log.debug(employee.getSn()+"---------"+employee.getPassword());
            employee = sysEmployeeService.Login(employee);
            if(employee.getName() != null && !employee.getName().equals("")){
                session.put(Constants.EMP_SESSION,employee);
                return SUCCESS;
            }else{
                session.put("msg","用户不存在");
                return INPUT;
            }
        }
        return INPUT;
    }
    public void validateLogin(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        if(employee.getSn() == null && employee.equals("") && employee.getSn().length()==0){
            session.put("msg","用户编号不能为空");
            return;
        }else if(employee.getPassword() == null && employee.getPassword().equals("") && employee.getPassword().length()==0){
            session.put("msg","用户密码不能为空");
            return;
        }
    }

    public String logout(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        log.debug("退出成功-------");
        session.remove(Constants.EMP_SESSION);
        return INPUT;
    }



    public SysEmployeeService getSysEmployeeService() {
        return sysEmployeeService;
    }

    public void setSysEmployeeService(SysEmployeeService sysEmployeeService) {
        this.sysEmployeeService = sysEmployeeService;
    }

    public SysEmployee getEmployee() {
        return employee;
    }

    public void setEmployee(SysEmployee employee) {
        this.employee = employee;
    }
}
